<template>
	<view class="page tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
		<view class="top-bg"></view>
		<dawnBar title="家庭成员"></dawnBar>
		<view class="page-container" style="padding-bottom: 140rpx;">
			<view class="order--wrap">
				<!-- 标签内容 -->
				<view class="list">
					<view class="list-item" v-for="(item,index) in list" :key="index">
						<view class="list-head tn-flex tn-flex-direction-row tn-flex-col-center tn-flex-row-between">
							<view @click="navDteail(item)"
								class="tn-flex tn-flex-direction-row tn-flex-col-center tn-flex-row-between">
								<!-- <text
									class="dawn-text-md tn-text-bold tn-color-dawn tn-padding-right-xs">{{item.name_ || '' }}</text> -->
								<tn-tag class="dawn-tag-sm">{{item.patientSex || '' }}</tn-tag>
								<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
								<tn-tag class="dawn-tag-sm dawn-tag-sm__1">{{item.patientAge || '' }}</tn-tag>
								<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
								<tn-tag class="dawn-tag-sm dawn-tag-sm__2">{{item.nation || '' }}</tn-tag>
							</view>
							<view>
								<view class="justify-content-item ">
									<tn-tag backgroundColor="tn-main-gradient-dawn" fontColor="#FFFFFF"
										shape="circleLeft" width="auto">
										{{ item.phone_ || ''}}
									</tn-tag>
								</view>
							</view>
						</view>
						<view class="list-content tn-flex tn-flex-nowrap tn-flex-col-center tn-flex-row-between">
							<view class="tn-flex tn-flex-nowrap  tn-flex-col-center tn-flex-row-left">
								<view @click="navDteail(item)">
									<tn-avatar shape="square" :text="item.name" size="130rpx"></tn-avatar>
									<!-- <image mode="aspectFill" class="hosp-image" :src="userInfo.avatar"> -->
								</view>
								<view class="tn-padding-left-ss">
									<view class="tn-margin-top-xs">
										<text class="tn-icon-image-text-fill dawn-text-sm tn-margin-right-ss"></text>
										<text class="dawn-text-sm">{{ item.patientId }}</text>
										<text class="tn-color-gray tn-padding-left-xs tn-padding-right-xs">|</text>
										<text class="tn-icon-ipad-fill dawn-text-sm tn-margin-right-ss"></text>
										<text class="dawn-text-sm">{{ item.clinicNo }}</text>
									</view>
									<view class="tn-margin-top-xs">
										<text class="tn-icon-identity-fill dawn-text-sm tn-margin-right-ss"></text>
										<text class="dawn-text-sm">{{ item.cardNo_ }}</text>
									</view>
									<view class="tn-margin-top-xs">
										<text class="tn-icon-map-fill dawn-text-sm tn-margin-right-ss"></text>
										<text class="dawn-text-sm">{{ item.address }}</text>
									</view>
								</view>
							</view>
							<view @click="navDteail(item)"
								class="tn-flex tn-flex-nowrap  tn-flex-col-center tn-flex-row-left">
								<view class="justify-content-item tn-text-right">
									<text class="tn-icon-right tn-color-gray"></text>
								</view>
							</view>
						</view>

						<view class="dawn-line tn-margin-top-sm"></view>
						<view
							class=" tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-between">
							<view class="justify-content-item tn-text-df">
								<text class="tn-icon-time-fill tn-color-black tn-margin-right-ss"></text>
								<text class="tn-color-black">{{ formatDateTime(item.createTime)  || '' }}</text>
							</view>
							<view
								class="tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-right op-box">
								<tn-button @click="navDel(item)" size="sm" class="dawn-btn-sm dawn-btn-sm__danger">
									<text class="tn-icon-delete-fill">解绑</text>
								</tn-button>
								<!-- <tn-button @click="navLink('/hosp/patient/form?itemId='+item.id)" size="sm" class="dawn-btn-sm dawn-btn-sm__primary">
									<text class="tn-icon-my-circle-fill">默认</text>
								</tn-button> -->
							</view>
						</view>
					</view>
				</view>

				<!-- 悬浮按钮-->
				<view class="tn-flex tn-footerfixed">
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button @click="getPage()" class="dawn-btn-lg dawn-btn-lg__main"
							backgroundColor="tn-main-gradient-dawn">
							<text class="tn-icon-search-list-fill tn-padding-right-xs"></text>
							<text>查 询</text>
						</tn-button>
					</view>
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button @click="nav('/hosp/patient/create?toAuth=1')" class="dawn-btn-lg dawn-btn-lg__info"
							style="width: 85% !important;">
							<text class="tn-icon-add-square-fill tn-padding-right-xs"></text>
							<text>新 增</text>
						</tn-button>
					</view>
				</view>
			</view>
			<view v-if="show">
				<dawnMsg :box="dawnMsgBox" :msg="msg"></dawnMsg>
			</view>
		</view>

		<!-- loading -->
		<dawnLoading :show="loading"></dawnLoading>

		<!-- 分页 -->
		<!-- <dawn-pagination ref="pagination" :current="queryData.pageNo" :total="total" @pagination="getPage" /> -->

		<!-- 悬浮按钮-->
		<NavBtn></NavBtn>
	</view>
</template>

<script>
	import page_mixin from '@/mixins/page_mixin.js'
	import dayjs from '@/plugin/dayjs/dayjs.min.js';
	import * as PatientApi from '@/api/hospital/patient.js';
	import * as UserPatient from '@/libs/userPatient.js';
	import * as api from '@/api/api.js';
	import * as config from "@/api/config.js";
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	const app = getApp();

	export default {
		name: 'order',
		mixins: [page_mixin],
		computed: mapGetters(['appConfig', 'uid', 'isLogin', 'userInfo', 'patientInfo']),
		data() {
			return {
				loading: false,
				show: false,
				msg: '',
				dawnMsgBox: '',
				queryData: {
					pageSize: 10,
					pageNo: 1,
					userId: ''
				},
				list: []
			}
		},
		onLoad(options) {
			if (!this.isLogin) {
				toLogin();
				return;
			} else {
				this.getPage();
			}
		},
		methods: {
			async getPage(page) {
				let that = this;
				this.loading = true;
				this.show = false;
				this.dawnMsgBox = '';
				this.$set(this, 'list', []);
				let res = await UserPatient.getUserPatient(this.userInfo.openid);
				console.table(res);
				this.loading = false;
				if(res.code === 0){
					const l = res.data || [];
					this.$set(this, 'list', l);
					if (l.length == 0) {
						this.show = true;
					}
				}else {
					this.show = true;
					this.msg = res.msg;
				}
			},
			navDteail(item) {
				// app.globalData.dataText = this.dataText;
				// uni.navigateTo({
				// 	url: '/hosp/outpatient/unpay_recipe?registerNo=' + item.NO
				// })
				this.$util.setStorageData(config.STORE_KEY.P_DETAIL, {
					'itemId': item.id
				});
				uni.navigateTo({
					url: '/hosp/patient/detail'
				})
			},
			navDel(item) {
				let that = this;
				uni.showModal({
					title: '系统提示',
					content: '请确认是否要解绑?',
					success: function(res) {
						if (res.confirm) {
							that.navDelConfirm(item);
						} else if (res.cancel) {
							// console.log('取消');
							//TODO
						}
					}
				});
			},
			navDelConfirm(item) {
				let that = this;
				this.loading = true;
				this.show = false;
				this.dawnMsgBox = 'popup';
				const cancelData = new Object();
				cancelData.patientId = item.patientId;
				cancelData.patientName = item.name;
				cancelData.id = item.id;
				PatientApi.del(cancelData).then(res => {
					that.loading = false;
					console.table(res);
					if (res.code === 0) {
						that.$util.Tips({
							title: '解绑档案成功'
						});
						that.getPage();
					} else {
						that.show = true;
						that.msg = res.msg;
					}
				}).catch(err => {
					that.loading = false;
					that.show = true;
					that.msg = err.message;
				})
			},
			formatDate: function(date) {
				return dayjs(date).format("YYYY-MM-DD");
			},
			formatDateTime: function(date) {
				return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tn-avatar {
		z-index: 0 !important;
	}
</style>